package com.cms.controller.admin;

import com.cms.Feedback;
import com.cms.entity.Integration;
import com.cms.entity.Member;
import com.cms.routes.RouteMapping;
import org.apache.commons.lang.StringUtils;

import java.util.Date;
import java.util.HashMap;


/**
 * Controller - 会员积分
 * 
 * 
 * 
 */
@RouteMapping(url = "/admin/integration")

public class IntegrationController extends BaseController{

	/**
	 * 添加
	 */
	public void add(){
		String mobile = getPara("mobile");
		setAttr("mobile", mobile);
		setAttr("totalIntegration", new Integration().dao().getIntegration(mobile));
	    render(getView("integration/add"));
	}
	
	/**
	 * 保存
	 */
	public void save(){
		Integration integration = getModel(Integration.class,"",true);
		integration.setCreateTime(new Date());
		integration.setModifyTime(new Date());
		integration.setStatus(1);
		integration.save();
		redirect(getListQuery("/admin/integration/list?mobile="+integration.getMobile()));
	}
	
	
	/**
	 * 编辑
	 */
	public void edit(){
		Long id = getParaToLong("id");
		setAttr("integration", new Integration().dao().findById(id));
		render(getView("integration/edit"));
	}

	/**
	 * 更新
	 */
	public void update(){
//	    Long id = getParaToLong("id");
//	    String password = getPara("password");
//	    String username = getPara("username");
////		Member mem = getModel(Member.class);
//		Member member = new Member().dao().findById(id);
//	    if(StringUtils.isNotBlank(password)){
//	        member.setPassword(DigestUtils.md5Hex(password));
//	    }
//		if(StringUtils.isNotBlank(username)){
//			member.setUsername(username);
//		}
//		member.update();
//		redirect(getListQuery("/admin/member/list"));
	}
	
	/**
	 * 列表
	 */
	public void list(){
	    String mobile = getPara("mobile");
	    Integer pageNumber = getParaToInt("pageNumber");
        if(pageNumber==null){
            pageNumber = 1;
        }
        if(!StringUtils.isEmpty(mobile)){
			Long totalIntegration = new Integration().dao().getTotalIntegration(mobile);
			Long totalUsedIntegration = new Integration().dao().getTotalUsedIntegration(mobile);
			setAttr("page", new Integration().dao().findPage(mobile,pageNumber,PAGE_SIZE));
			setAttr("mobile", mobile);
			setAttr("totalIntegration", totalIntegration - totalUsedIntegration);
		}
		render(getView("integration/list"));
	}
	
	/**
	 * 删除
	 */
	public void delete(){
		Long ids[] = getParaValuesToLong("ids");
		for(Long id:ids){
			new Member().dao().deleteById(id);
		}
		renderJson(Feedback.success(new HashMap<>()));
	}
}
